<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'loanName', validatorRules.loanName]" placeholder="请输入贷款人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证"></a-input>
            </a-form-item>

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['fwzId',validatorRules.fwzId]" :trigger-change="true"
                                 dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择地区"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="贷款业务号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'loanNumber', validatorRules.loanNumber]" placeholder="请输入贷款业务号码"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款银行类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['loanBankType',validatorRules.loanBankType]"
                                 :trigger-change="true" dictCode="bank_type" placeholder="请选择贷款银行类型"/>
            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="贷款银行名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <!-- <j-dict-select-tag type="list" v-decorator="['loanBankId',validatorRules.loanBankId]"
                                 :trigger-change="true" dictCode="tb_dict_bank,bank_name,id" placeholder="请选择贷款银行名称"/>-->
              <a-input v-decorator="[ 'loanBankId', validatorRules.loanBankId]" placeholder="请输入贷款银行名称"></a-input>
              <!--<a-cascader
                :options="options"
                style="width: 100%"
                @change="onChange"
                :value="cascaderValue"
                placeholder="请选择贷款银行"
              />-->
            </a-form-item>

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  50000元格式：50000 &nbsp &nbsp 单位:元
                </template>
                <a-input v-decorator="[ 'loanMoney', validatorRules.loanMoney]" placeholder="请输入贷款金额"></a-input>
              </a-tooltip>

            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="借款日" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择借款日" v-decorator="[ 'borrowingDate', validatorRules.borrowingDate]"
                      :trigger-change="true" style="width: 100%"/>
            </a-form-item>

          </a-col>
        </a-row>


        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="到期日" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择到期日" v-decorator="[ 'endDate', validatorRules.endDate]" :trigger-change="true"
                      style="width: 100%"/>
            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="贷款用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['loanUse',validatorRules.loanUse]" :trigger-change="true"
                                 dictCode="loan_use" placeholder="请选择贷款用途"/>
            </a-form-item>

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款保证保险" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isLoanInsurance',validatorRules.isLoanInsurance]"
                                 :trigger-change="true" dictCode="is_loan_insurance" placeholder="请选择是否购买贷款保证保险"/>
            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="年利率%" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  例如:4.35
                </template>
                <a-input v-decorator="[ 'interestRates', validatorRules.interestRates]" placeholder="请输入年利率"></a-input>
              </a-tooltip>

            </a-form-item>

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贴息利率%" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  例如:4.35
                </template>
                <a-input v-decorator="[ 'compensationInterestRate', validatorRules.compensationInterestRate]"
                         placeholder="请输入贴息利率"></a-input>
              </a-tooltip>

            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="应贴息金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  200元格式：200 &nbsp &nbsp 单位:元
                </template>
                <a-input v-decorator="[ 'compensationMoney', validatorRules.compensationMoney]"
                         placeholder="请输入应贴息金额"></a-input>
              </a-tooltip>

            </a-form-item>

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="信用等级评定" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isEvaluation',validatorRules.isEvaluation]"
                                 :trigger-change="true" dictCode="is_evaluation" placeholder="请选择是否参加过信用等级评定"/>
            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="贷款类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['loanType',validatorRules.loanType]" :trigger-change="true"
                                 dictCode="loan_type" placeholder="请选择贷款类型"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="贷款模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['loanMode',validatorRules.loanMode]" :trigger-change="true"
                                 dictCode="loan_mode" placeholder="请选择贷款模式"/>
            </a-form-item>

          </a-col>
          <a-col :lg="12">
            <a-form-item label="定稿日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择定稿日期" v-decorator="[ 'finalizedDate', validatorRules.finalizedDate]" :trigger-change="true"
                      style="width: 100%"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction, getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "SmallLoanAccountModal",
        components: {
            JDate,
            JDictSelectTag,
        },
        data() {
            return {
                processId:"",
                cascaderValue: [],
                options: [],
                form: this.$form.createForm(this),
                title: "操作",
                width: 1000,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    fwzId: {rules: [{ required: true, message: '乡镇不能为空'}]},
                    loanNumber: {rules: [{ required: true, message: '贷款业务号码不能为空'}]},
                    loanName: {rules: [{ required: true, message: '贷款人姓名不能为空'}]},
                    idNumber: {rules: [{ required: true, message: '身份证号不能为空'}]},
                    loanBankType: {rules: [{ required: true, message: '贷款银行类型不能为空'}]},
                    loanBankId: {rules: [{ required: true, message: '贷款银行名称不能为空'}]},
                    loanMoney: {rules: [{ required: true, message: '贷款金额不能为空'}]},
                    borrowingDate: {rules: [{ required: true, message: '借款日不能为空'}]},
                    endDate: {rules: [{ required: true, message: '到期日不能为空'}]},
                    loanUse: {rules: [{ required: true, message: '贷款用途不能为空'}]},
                    isLoanInsurance: {rules: [{ required: true, message: '请选择是否购买贷款保险'}]},
                    interestRates: {rules: [{ required: true, message: '年利率不能为空'}]},
                    compensationInterestRate: {rules: [{ required: true, message: '贴息利率不能为空'}]},
                    compensationMoney: {rules: [{ required: true, message: '贴息金额不能为空'}]},
                    isEvaluation: {rules: [{ required: true, message: '信用等级评定不能为空'}]},
                    loanType: {rules: [{ required: true, message: '贷款类型不能为空'}]},
                    loanMode: {rules: [{ required: true, message: '贷款模式不能为空'}]},
                },
                url: {
                    add: "/loanmanagement/smallLoanAccount/add",
                    edit: "/loanmanagement/smallLoanAccount/edit",
                    queryByProcessId: "/loanmanagement/smallLoanAccount/queryByProcessId",
                }

            }
        },
        created() {
        },
        methods: {
            onChange(value, selectedOptions) {
                if (value.length > 0) {
                    this.cascaderValue = value
                    this.loanBankId = selectedOptions[1].code;
                    this.loanBankType=selectedOptions[0].code;
                }
            },
            add() {
                this.edit({});
                this.cascaderValue = []
            },
            edit(record) {
                const that = this
                getAction("/common/dictBank/bankCascadedDropDown", "").then((res) => {
                    if (res.success) {
                        this.options = res.result
                    }
                    this.loading = false;
                })
                if (JSON.stringify(record) != "{}") {
                    var params = {};
                    params.loanBankId = record.loanBankId;
                    getAction("/common/dictBank/getCascadedDropDownEchoResults", params).then((res) => {
                        if (res.success) {
                            that.cascaderValue = res.result
                        }
                        this.loading = false;
                    })
                }
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model,'finalizedDate', 'fwzId', 'loanNumber', 'loanName', 'idNumber', 'loanBankType', 'loanBankId', 'loanMoney', 'borrowingDate', 'endDate', 'loanUse', 'isLoanInsurance', 'interestRates', 'compensationInterestRate', 'compensationMoney', 'isEvaluation', 'loanType', 'loanMode', 'processId'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        // values.loanBankId = this.loanBankId;
                        // values.loanBankType=this.loanBankType;
                        /*if (!values.loanBankId) {
                            that.$message.warning("贷款银行名称不能为空");
                            return ;
                        }*/
                        that.confirmLoading = true;
                        values.processId=this.processId
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'fwzId','finalizedDate', 'loanNumber', 'loanName', 'idNumber', 'loanBankType', 'loanBankId', 'loanMoney', 'borrowingDate', 'endDate', 'loanUse', 'isLoanInsurance', 'interestRates', 'compensationInterestRate', 'compensationMoney', 'isEvaluation', 'loanType', 'loanMode'))
            },
            showGoodsGallery1(record) {
                this.processId = record;
                const that = this;
                var params = {};
                params.processId = record;
                getAction(this.url.queryByProcessId, params).then((res) => {
                    if (res.success) {
                        that.$message.success("回显数据");
                        // that.$emit('ok');
                        that.edit(res.result);
                    } else {
                        that.$message.warning("新增页面");
                        that.add();
                    }
                    this.loading = false;
                })
                this.visible = true
            },

        }
    }
</script>